package com.hyt.it.ogt.pj.data.service.kwgov.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.pj.data.mapper.kwgov.TimeRoomCandidateMapper;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.Candidate;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.Task;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.TaskArea;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.TaskPlace;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.TaskRoom;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.TimeRoomCandidate;
import com.hyt.it.ogt.pj.data.model.vo.kwgov.NowTestNowCandidateArrangeVO;
import com.hyt.it.ogt.pj.data.model.vo.kwgov.NowTestNowDataVO;
import com.hyt.it.ogt.pj.data.model.vo.kwgov.TestClassifyVO;
import com.hyt.it.ogt.pj.data.service.kwgov.IKwGovTaskService;
import com.hyt.it.ogt.pj.data.service.kwgov.IKwGovTimeRoomCandidateService;
import com.hyt.it.ogt.pj.data.utils.DateTimeUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 考试任务表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@Slf4j
@Service
public class KwGovTimeRoomCandidateServiceImpl extends BaseServiceImpl<TimeRoomCandidateMapper, TimeRoomCandidate> implements IKwGovTimeRoomCandidateService {

	@Resource
	private IKwGovTaskService iKwGovTaskService;

	@DS("kw_gov_read")
	@Override
	public NowTestNowDataVO getCandidateArrange(String taskId, String timeId) {
		//获取任务数据
		Task task = iKwGovTaskService.getTaskById(taskId);

		NowTestNowDataVO nowTestNowDataVO = new NowTestNowDataVO();
		nowTestNowDataVO.setBeginDate(DateTimeUtil.getDateTimeString(task.getBeginDate()));
		nowTestNowDataVO.setEndDate(DateTimeUtil.getDateTimeString(task.getEndDate()));
		nowTestNowDataVO.setExamName(task.getName());
		nowTestNowDataVO.setId(taskId);

		List<TestClassifyVO> testClassifyVOList = iKwGovTaskService.getTestClassify();
		setTestClassify(testClassifyVOList,task.getTestClassify(),nowTestNowDataVO);
		nowTestNowDataVO.setTestClassify(task.getTestClassify());


		//获取考生编排数据
		List<TimeRoomCandidate> timeRoomCandidateList = this.lambdaQuery()
				.eq(TimeRoomCandidate::getTaskId,taskId)
				.eq(TimeRoomCandidate::getTimeId,timeId)
				.eq(TimeRoomCandidate::getDelFlag,false)
				.list();

		Map<String, TaskRoom> mapTaskRoom = this.getTaskRoomMap(taskId);
		Map<String, TaskPlace> mapTaskPlace = this.getTaskPlaceMap(taskId);
		Map<String, TaskArea> mapTaskArea = this.getTaskAreaMap(taskId);

		List<NowTestNowCandidateArrangeVO> nowTestNowCandidateArrangeVOList = new ArrayList<>();
		timeRoomCandidateList.stream().forEach(timeRoomCandidate -> {
			NowTestNowCandidateArrangeVO nowTestNowCandidateArrangeVO = new NowTestNowCandidateArrangeVO();
			nowTestNowCandidateArrangeVO.setCandidateId(timeRoomCandidate.getCandidateId());
			nowTestNowCandidateArrangeVO.setSeatNo(timeRoomCandidate.getSeatNum());

			Candidate candidate = baseMapper.selectCandidateByCandidateId(timeRoomCandidate.getCandidateId());
			if (StringUtils.isNotBlank(candidate.getProvince())) {
				nowTestNowCandidateArrangeVO.setProvinceCode(candidate.getProvince());
				nowTestNowCandidateArrangeVO.setProvinceName(mapTaskArea.get(candidate.getProvince()).getName());
			}

			if (StringUtils.isNotBlank(candidate.getCity())) {
				nowTestNowCandidateArrangeVO.setCityCode(candidate.getCity());
				TaskArea mapTaskAreaTemp =  mapTaskArea.get(candidate.getCity());
				if(null != mapTaskAreaTemp) {
					nowTestNowCandidateArrangeVO.setCityName(mapTaskAreaTemp.getName());
				}
			}

			if (StringUtils.isNotBlank(candidate.getDistrict())) {
				nowTestNowCandidateArrangeVO.setDistrictCode(candidate.getDistrict());
				TaskArea mapTaskAreaTemp =  mapTaskArea.get(candidate.getDistrict());
				if(null != mapTaskAreaTemp) {
					nowTestNowCandidateArrangeVO.setDistrictName(mapTaskAreaTemp.getName());
				}
			}

			TaskRoom taskRoom = mapTaskRoom.get(timeRoomCandidate.getRoomId());
			nowTestNowCandidateArrangeVO.setRoomId(taskRoom.getId());
			nowTestNowCandidateArrangeVO.setRoomName(taskRoom.getName());

			TaskPlace taskPlace = mapTaskPlace.get(taskRoom.getPlaceId());
			nowTestNowCandidateArrangeVO.setPlaceId(taskPlace.getId());
			nowTestNowCandidateArrangeVO.setPlaceName(taskPlace.getName());

			nowTestNowCandidateArrangeVOList.add(nowTestNowCandidateArrangeVO);
		});

		nowTestNowDataVO.setNowTestNowCandidateArrangeVOList(nowTestNowCandidateArrangeVOList);
		return nowTestNowDataVO;
	}


	private void setTestClassify( List<TestClassifyVO> testClassifyVOList,String taskTestClassify,NowTestNowDataVO nowTestNowDataVO) {
		testClassifyVOList.stream().forEach(testClassifyVO -> {
			if (taskTestClassify.contains(testClassifyVO.getId())) {
				if (StringUtils.isBlank(nowTestNowDataVO.getTestClassifyName())) {
					nowTestNowDataVO.setTestClassifyName(testClassifyVO.getName());
				} else {
					nowTestNowDataVO.setTestClassifyName(StringUtils.join(nowTestNowDataVO.getTestClassifyName(),",",testClassifyVO.getName()));
				}


				if (testClassifyVO.getChildren() != null && !testClassifyVO.getChildren().isEmpty()) {
					setTestClassify(testClassifyVO.getChildren(),taskTestClassify,nowTestNowDataVO);
				}
			}
		});
	}

	private Map<String,TaskRoom> getTaskRoomMap(String taskId) {
		List<TaskRoom> taskRoomList = baseMapper.selectTaskRoom(taskId);
		Map<String,TaskRoom> map = new HashMap<>();
		taskRoomList.stream().forEach(taskRoom -> {
			map.put(taskRoom.getId(),taskRoom);
		});

		return map;
	}

	private Map<String,TaskPlace> getTaskPlaceMap(String taskId) {
		List<TaskPlace> taskPlaceList =  baseMapper.selectTaskPlace(taskId);
		Map<String,TaskPlace> map = new HashMap<>();
		taskPlaceList.stream().forEach(taskPlace -> {
			map.put(taskPlace.getId(),taskPlace);
		});

		return map;
	}

	private Map<String,TaskArea> getTaskAreaMap(String taskId){
		List<TaskArea> taskCandidateAreaList = baseMapper.selectTaskArea(taskId);
		Map<String,TaskArea> mapTaskAreaMap = new HashMap<>(taskCandidateAreaList.size());
		taskCandidateAreaList.forEach(taskArea -> mapTaskAreaMap.put(taskArea.getAreaId(),taskArea));
		return mapTaskAreaMap;
	}


}
